package com.yuyou.weixin.controller;

import com.yuyou.weixin.bean.ResBoolSimpleInfo;
import com.yuyou.weixin.enums.CommSmsVerCodeEnums;
import com.yuyou.weixin.service.ICommSmsVerCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.weixin.bean.ReturnResult;
import com.yuyou.weixin.service.ICaptchaService;
import com.yuyou.weixin.vo.resp.RespCaptcha;

@Api(value="AuthResourceController",description="验证码接口")
@Validated
@RestController
@RequestMapping(value="/api/captcha")
public class CaptchaController {


	@Autowired
	private ICaptchaService captchaService;

    @Autowired
    private ICommSmsVerCodeService commSmsVerCodeService;

	@ApiOperation(value="生成验证码,在img前加上data:image/jpeg;base64,")
    @RequestMapping(value ="/generCaptcha",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<RespCaptcha> generCaptcha() throws Exception{
    	RespCaptcha res=captchaService.generCaptcha();
        return ReturnResult.success(res); 
    }


	/**
	 * @param phone
	 * @param captcha
	 * @param token
	 * @return
	 */
    @ApiOperation("发送注册登录手机验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", paramType="query", required = true),
            @ApiImplicitParam(name = "captcha", value = "图形验证码", paramType="query",required = true),
            @ApiImplicitParam(name = "token", value = "图形验证码token", paramType="query", required = true)}
    )
    @RequestMapping(value="/sendLoginSms",method=RequestMethod.POST)
    public ReturnResult<ResBoolSimpleInfo<String>> addSmsVerCode(
            @NotBlank(message="phone不能为空")String phone,
            @NotBlank (message="captcha不能为空")String captcha,
            @NotBlank (message="token不能为空")String token){

        //图形验证码
        ResBoolSimpleInfo<String> captchaRes=captchaService.verifyCode(token, captcha);
        if(!captchaRes.isSuccess()){
            return ReturnResult.fail(captchaRes.getMessage());
        }
        ResBoolSimpleInfo<String> stringResBoolSimpleInfo = commSmsVerCodeService.addSmsVerCode(phone, CommSmsVerCodeEnums.type.regisAndReset.getValue());
        if(!stringResBoolSimpleInfo.isSuccess()){
            return  ReturnResult.fail(stringResBoolSimpleInfo.getMessage());
        }
        return ReturnResult.success(stringResBoolSimpleInfo.getMessage());
    }

	
	
}
